package com.wx.md.utils;

import java.util.Date;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 随机数工具
 *
 * @author 刘畅
 * @date 2018/8/17
 */
public class RandomUtils {
  private RandomUtils() {
  }

  /**  自增长索引 */
  private static final AtomicInteger INDEX = new AtomicInteger(0);
  /**  JVM机器码 */
  private static final int MACHINE_CODE = 1;
  private static final int MOL = 10;

  /**
   * 创建基于当前时间的随机id
   *
   * @return yyyyMMddHHmmss + 机器码+ DEVICE_ID_FILL_LENGTH 位序列
   */
  synchronized
  public static Long randomDateId() {
    Date date = new Date();
    String format = DateUtils.format(date, "yyyyMMddHHmmssSSS");
    String index = String.valueOf(Math.abs(INDEX.getAndIncrement() % MOL));
    return Long.parseLong(format + MACHINE_CODE+index);
  }

}
